<template>
   <div class="app-container">
      <div class="head-container">
         <a-form :model="queryParams" ref="queryRef" layout="inline">
            <a-form-item prop="postCode">
               <a-input v-model="queryParams.postCode" placeholder="请输入岗位编码" clearable style="width: 200px" />
            </a-form-item>
            <a-form-item prop="postName">
               <a-input v-model="queryParams.postName" placeholder="请输入岗位名称" clearable style="width: 200px" />
            </a-form-item>
            <a-form-item prop="status">
               <a-select v-model="queryParams.status" placeholder="岗位状态" clearable style="width: 200px">
                  <a-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label"
                     :value="dict.value" />
               </a-select>
            </a-form-item>
            <a-form-item>
               <a-space>
                  <a-button type="primary" @click="handleQuery">
                     <template #icon>
                        <icon-search />
                     </template>
                     搜索</a-button>
                  <a-button @click="resetQuery">
                     <template #icon>
                        <icon-refresh />
                     </template>
                     重置</a-button>
               </a-space>
            </a-form-item>
         </a-form>
      </div>
      <div class="main-container">
         <div class="main-toolbar">
            <a-space>
               <a-button type="primary" @click="handleAdd">
                  <template #icon>
                     <icon-plus />
                  </template>
                  新增</a-button>
               <a-button status="success" @click="handleUpdate">
                  <template #icon>
                     <icon-edit />
                  </template>
                  修改</a-button>
               <a-button status="danger" :disabled="selectedKeys.length === 0" @click="handleDelete">
                  <template #icon>
                     <icon-delete />
                  </template>
                  删除</a-button>
            </a-space>
         </div>
         <div class="main-content">
            <a-table :columns="columns" :data="postList" row-key="postId"
               :row-selection="{ type: 'checkbox', showCheckedAll: true }" @selection-change="handleSelectionChange">

            </a-table>
         </div>
      </div>
      <PostForm ref="postFormRef"></PostForm>
   </div>
</template>

<script setup name="Post">
import { addPost, delPost, getPost, listPost, updatePost } from "@/api/system/post";
import PostForm from "./postForm";
const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
   form: {},
   queryParams: {
      pageNum: 1,
      pageSize: 10,
      postCode: undefined,
      postName: undefined,
      status: undefined
   },
   rules: {
      postName: [{ required: true, message: "岗位名称不能为空", trigger: "blur" }],
      postCode: [{ required: true, message: "岗位编码不能为空", trigger: "blur" }],
      postSort: [{ required: true, message: "岗位顺序不能为空", trigger: "blur" }],
   }
});

const { queryParams, form, rules } = toRefs(data);

const columns = ref([
   { key: 0, title: "岗位编号", dataIndex: "postId" },
   { key: 1, title: "岗位编码", dataIndex: "postCode" },
   { key: 2, title: "岗位名称", dataIndex: "postName" },
   { key: 3, title: "岗位排序", dataIndex: "postSort" },
   { key: 4, title: "状态", dataIndex: "status" },
   { key: 5, title: "创建时间", dataIndex: "createTime" },
])
const selectedKeys = ref([])
/** 查询岗位列表 */
function getList() {
   loading.value = true;
   listPost(queryParams.value).then(({ data }) => {
      postList.value = data.rows;
      total.value = data.total;
      loading.value = false;
   });
}
/** 取消按钮 */
function cancel() {
   open.value = false;
   reset();
}
/** 表单重置 */
function reset() {
   form.value = {
      postId: undefined,
      postCode: undefined,
      postName: undefined,
      postSort: 0,
      status: "0",
      remark: undefined
   };
   proxy.resetForm("postRef");
}
/** 搜索按钮操作 */
function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
}
/** 重置按钮操作 */
function resetQuery() {
   proxy.resetForm("queryRef");
   handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
   selectedKeys.value = selection
}
/** 新增按钮操作 */
function handleAdd() {
   reset();
   open.value = true;
   title.value = "添加岗位";
}
/** 修改按钮操作 */
function handleUpdate(row) {
   reset();
   const postId = row.postId || ids.value;
   getPost(postId).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改岗位";
   });
}
/** 提交按钮 */
function submitForm() {
   proxy.$refs["postRef"].validate(valid => {
      if (valid) {
         if (form.value.postId != undefined) {
            updatePost(form.value).then(response => {
               proxy.$modal.msgSuccess("修改成功");
               open.value = false;
               getList();
            });
         } else {
            addPost(form.value).then(response => {
               proxy.$modal.msgSuccess("新增成功");
               open.value = false;
               getList();
            });
         }
      }
   });
}
/** 删除按钮操作 */
function handleDelete(row) {
   const postIds = row.postId || ids.value;
   proxy.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(function () {
      return delPost(postIds);
   }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
   }).catch(() => { });
}
/** 导出按钮操作 */
function handleExport() {
   proxy.download("system/post/export", {
      ...queryParams.value
   }, `post_${new Date().getTime()}.xlsx`);
}

getList();
</script>
